#include "tool_http.h"
#include <QDebug>

Tool_http::Tool_http(QObject *parent) : QObject(parent)
{
    getDevicePosition();
    setRequestUrl("http://chinagpr.com?hostNumber=M196060143&hotName=asd&deviceIp=10.15.12.1&hotPassword=asdas&antennaType=LTD-2600&activeLongitude=118.56&activeLatitude=45.6&factoryTime=2021-11-05",true);
}

Tool_http::~Tool_http()
{
    if(m_source!=nullptr){
        m_source->deleteLater();
    }
}
/*!
 * \brief Tool_http::setdevice  认证并激活设备
 * \param url
 */
void Tool_http::setRequestUrl(const QString qrres,const bool isTemporay)
{
    QUrl url(qrres);

    //认证地址：https://172.18.23.95:8080/device/info/credic
    /*hostNumber=M196060143
     * &hotName=asd
     * &deviceIp=10.15.12.1
     * &hotPassword=asdas
     * &antennaType=LTD-2600
     * &activeLongitude=118.56
     * &activeLatitude=45.6
     * &factoryTime=2021-11-05*/

    //构造认证url
    //临时授权地址：https://172.18.23.95:8080/device/info/autho?
    //hostNumber=M196060143
    //&activeLongitude=118.56
    //&activeLatitude=45.6

/*
 * 永久授权地址  172.18.23.95:8080/device/info/perpetualAutho
 * ?hostNumber=M196060143
 * &perpetual=1
 * &activeLongitude=118.56
 * &activeLatitude=45.6
*/


    qDebug()<<"Tool_http::setdevice  url"<<url;
    url.setScheme("https");
    url.setAuthority("172.18.23.95:8080");
    url.setPath("/device/info/autho");

    QString  str;
    str=url.toString();

    qDebug()<<"Tool_http::setdevice  url"<<url.isEmpty();
    qDebug()<<"Tool_http::setdevice  str"<<str;
}

/*!
 * \brief Tool_http::getDevicePosition
 */
void Tool_http::getDevicePosition()
{
    if(m_source == nullptr)
    {
        m_source = QGeoPositionInfoSource::createDefaultSource(this);
        if (m_source) {
            connect(m_source,&QGeoPositionInfoSource::positionUpdated,
                    [=](const QGeoPositionInfo &update){
                m_position.setLongitude(update.coordinate().longitude());
                m_position.setLatitude(update.coordinate().latitude());
            });

            connect(m_source,&QGeoPositionInfoSource::updateTimeout,[=]{
                m_source->requestUpdate();
            });

            m_source->requestUpdate();
        }
    }else{
        m_source->requestUpdate();
    }
}
